package com.woniuxy.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.dto.UserRelatedDto.UserTableDetails;
import com.woniuxy.entity.UserTable;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.PawnUserException;
import com.woniuxy.service.UserTableService;

import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 登录接口
 */
@Service
public class LoadAccountServiceImpl implements UserDetailsService {

    @Resource
    private UserTableService uts;

    @Resource
    private StringRedisTemplate srt;


    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        UserTable user = uts.getOne(new QueryWrapper<UserTable>().eq("user_account", name)
                .eq("user_del",0));

        if (user == null) {
            throw new UsernameNotFoundException("没有此账号");
        }

        if (user.getUserStatus() == 1) {
            throw new PawnUserException(CodeEnum.LOGIN_FAIL.getMessage(), CodeEnum.LOGIN_FAIL.getCode());
        }


        UserTableDetails userTableDetails = new UserTableDetails();


        BeanUtils.copyProperties(user, userTableDetails);
        
        return userTableDetails;
    }
}
